package com.gc.server.controller.api;

import com.gc.server.entity.RouteEntity;
import com.gc.server.entity.SchoolEntity;
import com.gc.server.entity.ShopEntity;
import com.gc.server.service.RouteService;
import com.gc.server.service.SchoolService;
import com.gc.server.service.ShopService;
import com.gc.server.vo.RouteVo;
import com.gc.server.vo.ShopVo;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import okhttp3.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/route")
public class ApiRouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 路线列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserId Long userId) {
        List<RouteEntity> list = routeService.findByUserId(userId);
        List<RouteVo> routeVos = list.stream().map(l -> {
            RouteVo routeVo = new RouteVo();
            routeVo.setRoute(l);
            List<SchoolEntity> schoolEntities = schoolService.findByRouteId(l.getId());
            routeVo.setSchools(schoolEntities);
            List<ShopEntity> shopEntities = shopService.findByRouteId(l.getId());
            routeVo.setShops(shopEntities);
            return routeVo;
        }).collect(Collectors.toList());
        return R.ok(routeVos);
    }

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ShopService shopService;

    @GetMapping("/id")
    public R get(Long id) {
        RouteEntity routeEntity = routeService.get(id);
        List<SchoolEntity> schoolEntities = schoolService.findByRouteId(id);
        List<ShopEntity> shopEntities = shopService.findByRouteId(id);
        routeEntity.setSchoolIds(schoolEntities.stream().map(schoolEntity -> schoolEntity.getId()).collect(Collectors.toList()));
        routeEntity.setShopIds(shopEntities.stream().map(ShopEntity::getId).collect(Collectors.toList()));
        return R.ok(routeEntity).putExtra("shop", shopEntities).putExtra("school", schoolEntities);
    }

    /**
     * 添加路线
     *
     * @param userId
     * @param name
     * @param remark
     * @return
     */
    @PostMapping("/add")
    public R add(@LoginUserId Long userId, @RequestBody RouteEntity entity) {
        routeService.add(userId, entity);
        return R.ok();
    }

    @PostMapping("/shop")
    public R shop(Long userId,Long[] shopIds){
        shopService.add(userId,shopIds!=null?Arrays.asList(shopIds):new ArrayList<>());
        return R.ok();
    }

    @GetMapping("/shop")
    public R shopList(@LoginUserId Long userId){
        //找到我所有的学校
        List<ShopEntity> list = shopService.findByUserId(userId);
        //找到我已经在路线里的学校
        List<ShopEntity> tem = routeService.findShop(userId);
        List<Long> ids = tem.stream().map(m -> m.getId()).collect(Collectors.toList());
        //筛选没有的
        list = list.stream().filter(l -> !ids.contains(l.getId())).collect(Collectors.toList());
        return R.ok(list);
    }


    @PostMapping("/school")
    public R school(Long userId, Long[] schoolIds){
        schoolService.add(userId, schoolIds!=null?Arrays.asList(schoolIds):new ArrayList<>());
        return R.ok();
    }

    @GetMapping("/school")
    public R schoolList(@LoginUserId Long userId){
        //找到我所有的学校
        List<SchoolEntity> list = schoolService.findByUserId(userId);
        //找到我已经在路线里的学校
        List<SchoolEntity> tem = routeService.findSchool(userId);
        List<Long> ids = tem.stream().map(m -> m.getId()).collect(Collectors.toList());
        //筛选没有的
        list = list.stream().filter(l -> !ids.contains(l.getId())).collect(Collectors.toList());
        return R.ok(list);
    }

}
